package com.fund.main.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fund.main.config.GlobalExceptionHandler;
import com.fund.main.dao.TIncomeDao;
import com.fund.main.entity.TIncome;
import com.fund.main.service.TIncomeService;
import com.fund.utils.PageUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (TIncome)表服务实现类
 *
 * @author makejava
 * @since 2023-02-15 15:04:18
 */
@Service("tIncomeService")
public class TIncomeServiceImpl extends ServiceImpl<TIncomeDao, TIncome> implements TIncomeService {

    @Override
    public Page<TIncome> getPage(Page<TIncome> page, TIncome tIncome) {
        Page<TIncome> pageVo = new Page<>(page.getCurrent(), page.getSize() == 0L ? PageUtil.SIZE : page.getSize(), this.count());
        return this.page(pageVo, new QueryWrapper<TIncome>(tIncome));
    }

    /**
     * 根据名称模糊分页查询，待填入名称
     */
    @Override
    public Page<TIncome> getPageLikeName(Page<TIncome> page, TIncome tIncome) {
        Page<TIncome> pageVo = new Page<>(page.getCurrent(), page.getSize() == 0L ? PageUtil.SIZE : page.getSize(), this.count());
        return this.page(pageVo, new QueryWrapper<TIncome>().like(StringUtils.isNotBlank(""), "", tIncome));
    }

    @Override
    public Boolean insertOne(TIncome tIncome) {
        boolean save = this.save(tIncome);
        if (!save) {
            throw new GlobalExceptionHandler("保存失败");
        }
        return true;
    }

    @Override
    public Boolean updateOneById(TIncome tIncome) {
        boolean b = this.updateById(tIncome);
        if (!b) {
            throw new GlobalExceptionHandler("更新失败");
        }
        return true;
    }

    @Override
    public Boolean deleteListByIds(List<String> idList) {
        List<Long> collect = idList.stream().map(f -> Long.parseLong(f)).collect(Collectors.toList());
        boolean b = this.removeByIds(collect);
        if (!b) {
            throw new GlobalExceptionHandler("删除失败");
        }
        return true;
    }

    @Override
    public TIncome getOneById(String id) {
        return this.getById(Long.parseLong(id));
    }

    @Override
    public List<TIncome> queryAll(TIncome tIncome) {
        return this.list(new QueryWrapper<TIncome>(tIncome));
    }
}

